﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Dataglot;

namespace IrishSettings
{
    public class SelectUsersSettings:GenericCommand
    {
        public SelectUsersSettings(DataFactory factory)
            : base(factory)
        {
            const string sql = @"select * from {$ns}_settings_values
     where name= @name
    AND application= @application
    AND scope='user'
    AND user_id= @user_id
    AND version = @version
   ";
            ComposeSql(sql);
            AddParameter("@name", DbType.String);
            AddParameter("@application", DbType.String);
            AddParameter("@user_id", DbType.Int32);
            AddParameter("@version", DbType.String);
        }

        public DbDataReader SelectOne(
            string name,
            string application,
            int userId,
            Version version
            )
        {
            if(string.IsNullOrEmpty(name))
                throw new ArgumentException("Missing setting name","name");
            if (string.IsNullOrEmpty(application))
                throw new ArgumentException("Missing application name", "application");
            if(userId<=0)
                throw new ArgumentException("Expected nonzero userId", "userId");
            if (version == null)
                throw new ArgumentException("Missing version", "version");

            command.Parameters["@name"].Value = name;
            command.Parameters["@application"].Value = application;
            command.Parameters["@user_id"].Value = userId;
            command.Parameters["@version"].Value = version.ToString();
            return ExecuteReader(CommandBehavior.SingleRow);
        }
    }
}
